package org.shangrila.app.sys.mb;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;
import org.shangrila.app.sys.dao.SysDao;
import org.shangrila.app.sys.eo.SysMenus;
import org.shangrila.app.sys.eo.SysResource;

@Named
@SessionScoped
public class MenuBean {

	private MenuModel model;

	@PostConstruct
	public void initMenuInfo() {

		SysDao sysdao = new SysDao();
		List<SysMenus> menuItems = sysdao.findSysMenusAll();//获取全部菜单信息 ID=PID--->SubMenu ID<>PID--->MenuItem
		List<SysMenus> menus = sysdao.findSysMenus(); //获取菜单（下面包含菜单项）ID=PID--->SubMenu

		model = new DefaultMenuModel();

		for (SysMenus m : menus) {//循环菜单
			DefaultSubMenu submenu = new DefaultSubMenu(m.getName());
			for (SysMenus mi : menuItems) {//循环菜单项
				if ( (m.getId() == mi.getPid() ) &&  (mi.getPid()!=mi.getId() ) ) {//如果菜单记录的PID与菜单的ID相等的是菜单项
					DefaultMenuItem item = new DefaultMenuItem(mi.getName());
					for (SysResource r : mi.getSysAuthority().getSysResources()) {//获取菜单项的URL和ICON
						if ("1".equalsIgnoreCase(r.getCategory())) {
							item.setUrl(r.getSource());
							item.setIcon(r.getIcon());
						}
					}
					submenu.addElement(item);
				}
			}
			model.addElement(submenu);
		}
	}

	public void init() {
		model = new DefaultMenuModel();

		// First submenu
		DefaultSubMenu firstSubmenu = new DefaultSubMenu("动态菜单");

		DefaultMenuItem item = new DefaultMenuItem("PF主页");
		item.setUrl("http://www.primefaces.org");
		item.setIcon("ui-icon-home");
		firstSubmenu.addElement(item);

		model.addElement(firstSubmenu);

		// Second submenu
		DefaultSubMenu secondSubmenu = new DefaultSubMenu("动态动作");

		item = new DefaultMenuItem("保存");
		item.setIcon("ui-icon-disk");
		item.setCommand("#{menuBean.save}");
		item.setUpdate("messages");
		secondSubmenu.addElement(item);

		item = new DefaultMenuItem("删除");
		item.setIcon("ui-icon-close");
		item.setCommand("#{menuBean.delete}");
		item.setAjax(false);
		secondSubmenu.addElement(item);

		item = new DefaultMenuItem("Redirect");
		item.setIcon("ui-icon-search");
		item.setCommand("#{menuBean.redirect}");
		secondSubmenu.addElement(item);

		model.addElement(secondSubmenu);
	}

	public MenuModel getModel() {
		return model;
	}

	public void save() {
		addMessage("Success", "Data saved");
	}

	public void update() {
		addMessage("Success", "Data updated");
	}

	public void delete() {
		addMessage("Success", "Data deleted");
	}

	public void addMessage(String summary, String detail) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
}